#!/bin/bash
# *************************************
# 功能: Kubernetes集群基础环境
# 作者: 王树森
# 联系: wangshusen@sswang.com
# 版本: v0.1
# 日期: 2023-12-12
# *************************************

# K8s内核参数配置
k8s_kernel_config(){
  # 接收参数
  local ip_list="$1"
  # 执行逻辑
  if [ -f "${scripts_dir}/${base_kernel_scripts}" ]; then
    scp_file ${ip_list} "${scripts_dir}/${base_kernel_scripts}" "/tmp/"
    for i in ${ip_list};do
      ssh "${login_user}"@"${i}" "/bin/bash /tmp/${base_kernel_scripts}"
      echo -e "\e[32mK8s ${i} 节点主机内核参数调整完毕!!!\e[0m"
    done
  else
    echo -e "\e[31m脚本 ${base_kernel_scripts} 文件不存在，请确认!!\e[0m"
  fi
}

# Cri-dockerd环境部署
cri_dockerd_offline(){
  # 接收参数
  local ip_list="$1"
  # 执行逻辑
  echo -e "\e[33m开始执行cri服务软件的部署...\e[0m"
  if [ -f "${scripts_dir}/${base_cri_docker_scripts}" ]; then
    source "${scripts_dir}/${base_cri_docker_scripts}" "${ip_list}"
  else
    echo -e "\e[31m脚本 ${base_cri_docker_scripts} 文件不存在，请确认!!\e[0m"
  fi
}
# Docker环境部署
docker_deploy_online(){
  # 接收参数
  local ip_list="$1" 
  # 执行逻辑
  if [ -f "${scripts_dir}/${base_docker_scripts}" ]; then
    scp_file ${ip_list} "${scripts_dir}/${base_docker_scripts}" "/tmp/"
    for i in ${ip_list};do
      docker_status=$(ssh "${login_user}"@"${i}" "docker info" 2>/dev/null | grep 'p D' | awk '{print $NF}')
      if [ "${docker_status}" == "systemd" ]; then
        echo -e "\e[32mK8s ${i} 节点主机Docker软件已部署成功\e[0m"
      else
        ssh "${login_user}"@"${i}" "/bin/bash /tmp/${base_docker_scripts}"
        echo -e "\e[32mK8s ${i} 节点主机docker环境部署完毕!!!\e[0m"
      fi
    done
  else
    echo -e "\e[31m脚本 ${base_docker_scripts} 文件不存在，请确认!!\e[0m"
  fi
  # 调整docker的部署通用性，将cri的部署剔除docker的部署函数
  # cri_dockerd_offline "${ip_list}"
}

# Containerd环境

# CRI-O环境部署

# Harbor环境
harbor_deploy_offline(){
  # 接收参数
  local harbor_user="$1"
  local harbor_my_repo="$2"
  # 部署docker环境
  docker_deploy_online "${harbor_addr}" 
  # 执行逻辑
  if [ -f "${scripts_dir}/${base_harbor_install_scripts}" ]; then
    source "${scripts_dir}/${base_harbor_install_scripts}"
  else
    echo -e "\e[31m脚本 ${base_harbor_install_scripts} 文件不存在，请确认!!\e[0m"
  fi
}
# Keepalived环境

# Haproxy代理环境

# Nginx代理环境


# 一键集群基础环境
one_key_cluster_env(){
  echo -e "\e[33m开始执行K8s内核参数配置...\e[0m"
  k8s_kernel_config "${all_k8s_list}"
  echo -e "\e[33m开始执行docker软件的部署...\e[0m"
  docker_deploy_online "${all_k8s_list}"
  cri_dockerd_offline "${all_k8s_list}"
  echo -e "\e[33m开始执行容器镜像仓库部署...\e[0m"
  harbor_deploy_offline "${harbor_user}" "${harbor_my_repo}"
  # 判断k8s集群模式和集群类型
  if [ "${cluster_type}" != 'alone' ];then
    echo "执行高可用环境部署"
  fi
}
